const Promise = require('bluebird');

module.exports = function (storage) {
    let {modelsData: {Goodses}, search: {searchEnable, goodsesSearch}, taobaoApi} = storage;

    let convertGoods = function (pendingGoods) {
        return taobaoApi.getMaterial(pendingGoods.auctionId, pendingGoods.title).then(function (material) {
            if (material) {
                return Goodses.update(material, {where: {id: pendingGoods.id}}).then(function () {
                    return material;
                });
            }
            return material;
        }).then(function (material) {
            return taobaoApi.getCoupon(pendingGoods.auctionId, material && material.activityId || pendingGoods.activityId).then(function (coupon) {
                if (coupon) {
                    return Goodses.update(coupon, {where: {id: pendingGoods.id}});
                }
            });
        }).then(function () {
            return Goodses.findOne({where: {id: pendingGoods.id}}).then(function (goods) {
                let data = {
                    quanPrice: (goods.zkPrice - (goods.couponAmount || 0)).toFixed(2),
                    status: 'Active'
                }
                if (data.quanPrice <= 3) {
                    data.is30 = 1;
                }
                if (data.quanPrice <= 10) {
                    data.is99 = 1;
                }
                if (data.quanPrice <= 20) {
                    data.is199 = 1;
                }
                for (let i in data) {
                    goods[i] = data[i];
                }
                return Goodses.update(data, {where: {id: pendingGoods.id}}).then(function () {
                    if (searchEnable) {
                        goodsesSearch.index(goods);
                    }
                    return goods;
                });
            });
        }).catch(function (err) {
            if (err) {
                console.error('\n', err.stack || err.message);
            }
            return {};
        });
    }


    let convertGoodsesRuning = false;
    let convertGoodses = function () {
        if (convertGoodsesRuning) {
            return false;
        }
        convertGoodsesRuning = true;
        return Goodses.findAll({
            where: {status: 'Pending'},
            limit: 10,
            order: [['id', 'desc']],
            raw: true
        }).then(function (pendingGoodses) {
            return Promise.mapSeries(pendingGoodses || [], function (pendingGoods) {
                return convertGoods(pendingGoods);
            });
        }).then(function () {
            convertGoodsesRuning = false;
        }).catch(function (err) {
            convertGoodsesRuning = false;
            if (err) {
                console.error('\n', err.stack || err.message);
            }
        });
    }

    /**
     * 20秒转换一次商品
     */
    setInterval(convertGoodses, 1000 * 20);

    return {
        convertGoods: convertGoods,
        convertGoodses: convertGoodses
    }
};